package com.itcast.demo01.test;

import com.itcast.demo01.bean.Student;
import com.itcast.demo01.bean.TClass;
import com.itcast.demo01.bean.Teacher;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * @作者 itcast
 * @创建日期 2020/7/9 11:33
 **/
public class IocDemo01 {
    /**
     * 不使用IOC的解决方案
     * 学生: 小明
     * 班级: 3年2班
     * 教师: 陈老师
     */
    @Test
    public void test01(){
        Student student = new Student();
        student.setName("小明");

        TClass tClass = new TClass();
        tClass.setCname("3年2班");

        Teacher teacher = new Teacher();
        teacher.setTname("陈老师");

        student.settClass(tClass);
        tClass.setTeacher(teacher);

        student.sayHello();
    }
    /**
     * IOC 和 DI   IOC容器
     * IOC： 控制反转
     * DI： 依赖注入
     * 程序: 只需要关注业务
     * 使用IOC的解决方案
     */
    @Test
    public void test02(){
        // 创建IOC容器   IOC容器的初始化
        ApplicationContext context = new ClassPathXmlApplicationContext("demo01/applicationContext.xml");
        // getBean获取对象
        Student student = (Student)context.getBean("student");
        student.sayHello();
    }
    @Test
    public void test03(){
        // 创建IOC容器   IOC容器的初始化
        BeanFactory context = new XmlBeanFactory(new ClassPathResource("demo01/applicationContext.xml"));
        // getBean获取对象
        Student student = (Student)context.getBean("student");
        student.sayHello();
    }
}
